<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends BaseController {

	public function __construct() {
        parent::__construct();

        // 后台初始化方法
        $this->admin_init();
    }

	public function index() {
        // 如果是超管，就跳转到超管的控制器
        $userType = $this->session->rmxUser['user_type'];
        // var_export($userType);exit;
        if($userType == USER_TYPE_SUPER_ADMIN) {
            $redirectUrl = admin_url("admin");
            redirect($redirectUrl);
        }

        if($userType == USER_TYPE_SCHOOL || $userType == USER_TYPE_TEACHER) {
            $redirectUrl = admin_url("school");
            redirect($redirectUrl);   
        }
	}

    // 注销登录
    public function logout() {
        $this->session->set_userdata("rmxUser", NULL);
        $this->session->set_userdata("campusInfo", NULL);
        $this->session->set_userdata("campusList", NULL);
        // 如果是单点登录，去掉该session
        if($this->session->intensiveModule){
            $this->session->set_userdata("intensiveModule", NULL);
        }

        $redirectUrl = admin_url("login");
        redirect($redirectUrl);
    }

    // 修改密码
    public function password(){
        $this->rmxUser = $this->session->rmxUser;
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "password");
    }
    // 修改密码方法
    public function modify_password_action(){
        $username = $this->session->rmxUser['username'];
        $password = $this->post("old_password");   
        $newPassword = $this->post("new_password");
        $newPassword2= $this->post("new_password_2");

        // 查询数据库
        $this->load->model('accountModel');

        // 检查登录信息
        $sessionInfo = $this->session->rmxUser;
        $tableName = "";
        // 如果是超管
        if($sessionInfo['user_type'] == USER_TYPE_SUPER_ADMIN) {
            $tableName = AccountModel::$adminTable;
            $userInfo = $this->accountModel->admin_login($username, $password);
        }
        // 如果是学校用户
        if($sessionInfo['user_type'] == USER_TYPE_SCHOOL || $sessionInfo['user_type'] == USER_TYPE_TEACHER) {
            $tableName = AccountModel::$schoolUserTable;
            $userInfo = $this->accountModel->school_user_login($sessionInfo['school_id'], $username, $password);
        }

        if(!$userInfo) throw new Exception("登录密码错误，请确认后，重新输入");
        
        // 检查新密码
        if(!str_is_password($newPassword)) throw new Exception("请填写6-20个字符长度的密码");
        if(!$newPassword || $newPassword !== $newPassword2) throw new Exception("两次密码输入不一致，请重新输入");

        // 修改密码
        $save['password'] = password_hash($newPassword, PASSWORD_DEFAULT);
        $this->db->update($tableName, $save,"id='{$this->session->rmxUser['id']}'");

        $this->session->set_userdata("rmxUser", NULL);
        exit(json("修改密码成功，请重新登录", true));
    }

    // 切换校区
    public function select_campus($campusId){
        $campusList = $this->session->campusList;
        foreach($campusList as $campus) {
            // 如果有对应的校区，就切换校区
            if($campus['id'] == $campusId) {
                $this->session->set_userdata("campusInfo", $campus);
                $redirectUrl = admin_url("school");
                redirect($redirectUrl);
            }
        }

        show_error("不存在的校区，切换失败");
    }
}
